package com.hrbxlh.actionImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.ClassDao;
import com.hrbxlh.dao.CourseDao;
import com.hrbxlh.dao.HomeworkTestDao;
import com.hrbxlh.dao.MyCourseDao;
import com.hrbxlh.dao.RelStuClassDao;
import com.hrbxlh.dao.RelationDao;
import com.hrbxlh.util.DateUtil;
import com.hrbxlh.util.SessionValues;
import com.hrbxlh.util.pageUtil;
/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：RelationActionImpl.java
* Description：课程表排课actionImpl类
* History：
* Date           Author               Desc
* 2017.04.18	  xzx                  建立	
* -------------------------------------------------------
*/
@Component("relationActionImpl")
@Transactional
public class RelationActionImpl {

	@Resource(name = "relationDao")
	private RelationDao relationDao;
	
	@Resource(name = "myCourseDao")
	private MyCourseDao myCourseDao;
	
	@Resource(name = "classDao")
	private ClassDao classDao;
	
	@Resource(name = "courseDao")
	private CourseDao courseDao;

	@Resource(name = "paramActionImpl")
	private ParamActionImpl paramActionImpl;
	
	@Resource(name = "courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	@Resource(name = "relStuClassDao")
	private RelStuClassDao relStuClassDao;
	
	@Resource(name = "homeworkTestDao")
	private HomeworkTestDao homeworkTestDao;
	
	/**
	 * @description 查询这个教师或这个地点在某天某节课的课程信息
	 * @param Map<String,Object> paramMap  
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public List<Map<String, Object>> getOriginalClassTab(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			resultList = relationDao.getOriginalClassTab(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return resultList;
	}
	
	/**
	 * @description 保存手动排课
	 * @param Map<String,Object> paramMap  
	 * @return Map<String, String> result true成功，false失败failMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveClassTab(List<Map<String, Object>> paramList) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//获取上课时间的参数
			Map<String, Object> paramTimeMap = paramActionImpl.matchClassNumAndTime();
			//查询是否可以进行排课（地点是否冲突，教师是否有课）
			Map<String,Object> addMarkMap = this.getSaveTabMark(paramList);
			boolean saveMark = (Boolean)addMarkMap.get("result");
			//可以进行排课
			if(saveMark){
				Map<String, Object> classMap;
				//将修改后的课表集合替换
				//paramList = (List<Map<String, Object>>)addMarkMap.get("paramList");
				for(int i = 0;i < paramList.size(); i++){
					//获取申请班级的名称
					String className = this.getRandomClassName(paramList.get(i));
					//如果是教师排课，或者是学生选课并且有选课班级名称的进行排课
					if(className != null && Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2 || Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 1){
						//保存课程的标识（教师排课还是学生选课）
						courseActionImpl.saveSelectMark(paramList.get(i));
						//1、2、整理班级信息，存入课程表信息
						paramList.get(i).put("startTime", paramTimeMap.get((Integer)paramList.get(i).get("classNum")+"start"));
						paramList.get(i).put("endTime", paramTimeMap.get((Integer)paramList.get(i).get("classNum")+"end"));
						//如果是教师排课
						if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 1){
							paramList.get(i).put("addMark",1);
							//直接存入课程表
							relationDao.saveClassTab(paramList.get(i));
						//如果是选课	
						}else{
							//查询原来是不是有这个课程对应的选课班级信息（查询选课的班级有没有）
							List<Map<String, Object>> selectClassesList = relationDao.getSelectClass(paramList.get(i));
							if(selectClassesList != null && selectClassesList.size() != 0){
								paramList.get(i).put("classId", selectClassesList.get(0).get("classId"));
							//新建选课班级并将信息存入参数中	
							}else{
								classMap = new HashMap<String, Object>();
								classMap.put("className", className);
								relationDao.addSelectClass(classMap);
								paramList.get(i).put("classId", classMap.get("classId"));
							}
							//0504修改，保存选课的课表信息
							relationDao.saveClassTab(paramList.get(i));
						}
						//3、保存 课程与班级关联信息（注意是不是已经有了关联信息）
						List<Map<String, Object>> relCouClassList = relationDao.getRelCourseClass(paramList.get(i));
						//学生选课，保存 课程与班级关联信息（注意是不是已经有了关联信息）
						if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
							if(relCouClassList == null || relCouClassList.size() == 0){
								relationDao.saveRelCourseClass(paramList.get(i));
							}
						}

						//4、保存老师与课程的关系（注意给标识1或2） （注意是不是已经有了）
						List<Map<String, Object>> relCouTeaList = relationDao.getRelCourseTea(paramList.get(i));
						if(relCouTeaList == null || relCouTeaList.size() == 0){
							relationDao.saveRelCourseTea(paramList.get(i));
						}
						//0509添加的，有教师与课程的关系，将inMark修改为0
						else{
							paramList.get(i).put("inMark", 0);
							relationDao.saveInMarkReMark(paramList.get(i));
						}
						//学生选课，
						if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
							//5、教师排课，将班级下的所有学生与课程关系存入表中(如果原来存过就不存了，如果是选课的就不存了)
							//如果是教师排课
							if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 1){
								//查询原来是不是存在这个班级的学生的关联信息（如果有课程和班级的关联关系，就说明已经存在了，如果没有就是没有）
								if(relCouClassList == null || relCouClassList.size() == 0){
									//查询这个班级下的学生
									List<Map<String, Object>> stuList = relationDao.getStuByClassId(paramList.get(i));
									//0508如果学生是空的不添加
									if(stuList != null && stuList.size() != 0 ){
										Map<String, Object> stuReCourseMap ;
										for(int j = 0;j < stuList.size(); j++){
											stuReCourseMap = new HashMap<String, Object>();
											stuReCourseMap.put("stuorteaId", stuList.get(j).get("stuorteaId"));
											stuReCourseMap.put("courseId", paramList.get(i).get("courseId"));
											stuReCourseMap.put("selectMark", paramList.get(i).get("selectMark"));
											//查询这个学生这个课程之前有没有关联信息，如果没有就添加
											Map<String, Object> reListMap = relStuClassDao.getRelStuCouByStuCou(stuReCourseMap);
											if(reListMap == null){
												relationDao.saveRelCourseTea(stuReCourseMap);
											}		
										}
									}
								}
							}
							//6、教师与班级进行关联 （注意是不是已经有了）
							List<Map<String, Object>> relClassTeaList = relationDao.getRelClassTea(paramList.get(i));
							if(relClassTeaList == null || relClassTeaList.size() == 0){
								relationDao.saveRelClassTea(paramList.get(i));
							}
						}
						//修改课程的标识
						this.saveCourseSelect(paramList.get(i));
						//查询某个老师教的都是什么类型的课，如果有1的也有2的，就把这个人跟课的关联信息改为3
						paramList.get(i).put("myMark", 1);
						List<Map<String, Object>> oneList = relationDao.getRelationBySetMarkATid(paramList.get(i));
						paramList.get(i).put("myMark", 2);
						List<Map<String, Object>> twoList = relationDao.getRelationBySetMarkATid(paramList.get(i));
						//如果两种课程的课表都有，就改为3
						if(oneList != null && oneList.size() != 0 && twoList != null && twoList.size() != 0){
							paramList.get(i).put("selectMark", 3);
							relationDao.saveSelectMark(paramList.get(i));
						}
						//0616数据库如果只有2的课表
						if((oneList == null || oneList.size() == 0) && (twoList != null && twoList.size() != 0)){
							//0623如果与班级还不关联，不修改
							List<Map<String, Object>> relationList = relationDao.getTeachClassByTid(paramList.get(i));
							if(relationList != null && relationList.size() != 0){
								//0619修改老师的关系为选课、正常的类型3   （原来这里是2）
								paramList.get(i).put("selectMark", 3);
								relationDao.saveSelectMark(paramList.get(i));
							}else{
								//与班级不关联，就是只教选课的，改为2
								paramList.get(i).put("selectMark", 2);
								relationDao.saveSelectMark(paramList.get(i));
							}
						}
						//0616如果都为空，排的这个课是2就改为2
						if((oneList == null|| oneList.size() == 0) && (twoList == null || twoList.size() == 0)){
							if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
								
								//修改老师的关系为选课的类型
								paramList.get(i).put("selectMark", 2);
								relationDao.saveSelectMark(paramList.get(i));
							}
						}
						//0616结束
						resultMap.put("result", true);
					//选课班级名字重复
					}else{
						resultMap.put("result", false);
						resultMap.put("failMsg", "班级名称冲突");
						break;
					}
				}
			}else{
				resultMap.put("result", false);
				resultMap.put("failMsg", addMarkMap.get("failMsg"));
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 获取课表集合是否有冲突不可排课的标识 
	 * 1时间地点冲突，教师选择错误，3 正常排课的时候没有关联教师与课程的信息
	 * @param Map<String,Object> paramMap  
	 * @return （true可以排课  false不可排课）
	 * @throws Exception
	 */
	public Map<String, Object> getSaveTabMark(List<Map<String, Object>> paramList) throws Exception {
		
		boolean result = true;
		int originLength = paramList.size();
		int trueLength = 0;
		List<Map<String, Object>> list;
		String errorMsg = "";
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//查询有没有老师没有被关联在classrelation表中，如果有不让排课
			for(int i = 0; i < paramList.size(); i++){
				if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 1){
					//0703修改，要查询的是这个老师与这个课程在classrelation表中是否有记录，没有就算没有关联信息
					//List<Map<String, Object>> teachClassList = relationDao.getTeachClassByTid(paramList.get(i));
					List<Map<String, Object>> teachClassList = relationDao.getTeachClassByCidAndSid(paramList.get(i));
					if(teachClassList == null || teachClassList.size() == 0){
						result = false;
						errorMsg = "教师与课程关联信息未填加";
						break;
					}
				}
			}
			
			//2看选修的是不是安排了多个老师的问题
			if(result){
				//看是否某个课程选修的安排了2个老师的问题
				for(int i = 0; i < paramList.size(); i++){
					if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
						//查询这个课这个类型的其他上课老师有多少  还有3
						List<Map<String, Object>> teacherList = relationDao.getCourseTeaOtherList(paramList.get(i));
						if(teacherList != null && teacherList.size() != 0){
							result = false;
							errorMsg = "教师选择错误";
							break;
						}
					}
				}
			}
			
			//如果通过第一步
			if(result){
				//3看地点时间是否冲突
				for(int i = 0; i < paramList.size(); i++){
					list = relationDao.getOriginalClassTab(paramList.get(i));
					if(list == null){
						trueLength += 1;
					}else{
						if(list.size() == 0){
							trueLength += 1;
						}
					}	
				}
				if(originLength != trueLength){
					result = false;
					errorMsg = "时间地点冲突";
				}
			}
			resultMap.put("result", result);
			//将保存好教师id的集合传回去
			resultMap.put("paramList", paramList);
			if(!result){
				resultMap.put("failMsg", errorMsg);
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 保存自动排课
	 * @param Map<String,Object> paramMap  
	 * @return Map<String, String> result true成功，false失败failMsg错误信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> saveAutoClassTab(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		DateUtil dateUtil = new DateUtil();
		try {
			if(paramMap.get("courseId") == null || paramMap.get("stuorteaId") == null){
				resultMap.put("result", false);
				resultMap.put("failMsg", "请将排课信息补充完整");
			}else{
				//构造参数
				paramMap.put("startDate", paramMap.get("classDateStart"));
				paramMap.put("endDate", paramMap.get("classDateEnd"));
				//获取选择的时间段内的上课日期（满足星期几的条件的）
				Map<String, Object> dateMap = dateUtil.getWeekByDate(paramMap);
				//获取得到的全部日期集合
				List<String> dateList = (List<String>)dateMap.get("dateList");
				if(dateList.size() != 0){
					//用于存储可以进行排课的信息
					List<Map<String, Object>> okClassList = new ArrayList<Map<String,Object>>();
					Map<String, Object> useMap ;
					//获取要排课的位置课节数
					List<Integer> classNumList = (List<Integer>)paramMap.get("classNumList");
					for(int i = 0; i < dateList.size(); i++){
						for(int j = 0; j < classNumList.size(); j++){
							//构建参数
							useMap = new HashMap<String, Object>();
							useMap.put("classNum", classNumList.get(j));
							useMap.put("classDate", dateList.get(i));
							useMap.put("stuorteaId", paramMap.get("stuorteaId"));
							useMap.put("siteId", paramMap.get("siteId"));
							//查询是否有上课信息
							List<Map<String, Object>> originalTabList = relationDao.getOriginalClassTab(useMap);
							if(originalTabList == null || originalTabList.size() == 0){
								useMap.put("selectMark", paramMap.get("selectMark"));
								useMap.put("departmentId", paramMap.get("departmentId"));
								useMap.put("professionId", paramMap.get("professionId"));
								useMap.put("courseId", paramMap.get("courseId"));
								useMap.put("classId", paramMap.get("classId"));
								useMap.put("mainId", paramMap.get("mainId"));
								okClassList.add(useMap);
							}
						}
					}
					//如果存在可排课的课程
					if(okClassList.size() != 0){
						//保存排课信息
						Map<String, Object> addResult = this.saveClassTab(okClassList);
						if((Boolean)addResult.get("result")){
							resultMap.put("result", true);
						}else{
							resultMap.put("result", false);
							resultMap.put("failMsg", addResult.get("failMsg"));
						}
					}else{
						resultMap.put("result", false);
						resultMap.put("failMsg", "排课地点冲突或教师上课时间冲突");
					}
				}else{
					resultMap.put("result", false);
					resultMap.put("failMsg", "当前选择条件下并无可排课日期");
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询某时间范围内的地点的上课课表信息
	 * @param Map<String,Object> paramMap  
	 * @return 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getSiteClassTab(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			List<Map<String, Object>> dateList = (List<Map<String, Object>>)paramMap.get("dateList");
			paramMap.put("startDate", dateList.get(0));
			paramMap.put("endDate", dateList.get(dateList.size()-1));
			resultList = relationDao.getSiteClassTab(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return resultList;
	}
	
	/**
	 * @description 管理员的课程管理（排课表的列表可以搜索）
	 * @param Map<String,Object> paramMap  
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> getManagerCourseTabList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> tabList = relationDao.getManagerCourseTabList(paramMap);
			resultMap.put("tabList", tabList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = relationDao.getManagerCourseTabList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 删除某条课表信息
	 * @param Map<String,Object> paramMap  relationId
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> delClassTabById(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//查询这条课表全部信息
			Map<String, Object> relDetail = relationDao.getOneRelationById(paramMap);
			//存放这个班级下的学生
			List<Map<String, Object>> stuList = new ArrayList<Map<String,Object>>();
			stuList = relationDao.getStuByClassId(relDetail);
			//3、查询数据库课程表信息中全部这个课程的课表信息
			relDetail.put("type", 4);
			List<Map<String, Object>> relClassTeacherList = relationDao.getRelaById(relDetail);
			if(relClassTeacherList != null && relClassTeacherList.size() != 0){
				//如果课表长度为1，并且主键id是要删除的主键id，就剩一条了
				if((relClassTeacherList.size() == 1) && ((Integer.parseInt(relClassTeacherList.get(0).get("relationId").toString())) == (Integer.parseInt(relDetail.get("relationId").toString())))){
					//0509通过课程id查询这个课程的录入人
					Map<String, Object> courseDetail = relationDao.getInsertTeacherId(paramMap);
					courseDetail.put("inMark", 1);
					//如果录入人与这个课在班级里面有关联，不改inMark
					List<Map<String, Object>> attendList = relationDao.getClassRelationByCourseIdAndTeacherId(courseDetail);
					if(attendList == null || attendList.size() == 0){
						relationDao.saveInMarkReMark(courseDetail);
					}
					//0619如果剩最后一条需要把课程的标识修改为正常课程
					Map<String, Object> saveUseParam = new HashMap<String, Object>();
					saveUseParam.put("selectMark", 1);
					saveUseParam.put("courseId", relClassTeacherList.get(0).get("courseId"));
					courseDao.saveSelectMark(saveUseParam);
					//如果只剩最后一条的时候要把couSelectMark改为1
					saveUseParam.put("stuorteaId", relDetail.get("stuorteaId"));
					relationDao.saveSelectMark(saveUseParam);
					//0619end
					//如果这条是选修的
					if(Integer.parseInt(relDetail.get("selectMark").toString()) == 2){
						//删除学生与这个课的这个类型的关系
						relationDao.delStuReCourse(relDetail);
						//将选修这个班级与学生之间的关联删除
						Map<String, Object> detailMsg = new HashMap<String, Object>();
						detailMsg.put("courseId", relDetail.get("courseId"));
						detailMsg.put("myDelMark", 1);
						relationDao.delSelectRel(detailMsg);
						//0706删除这个选修班级
					    List<String> classList = new ArrayList<String>();
					    classList.add(relDetail.get("classId").toString());
						classDao.delClass(classList);
					}
				}else{
					//删除剩余最后一种类型的这个课的排课信息的时候，删除关联信息
					this.delByLastType(paramMap,relDetail,stuList);
				}
			}
			//如果录入老师没有教课信息了，需要将录入老师的inMark该回到1
			relDetail.put("type", 2);
			List<Map<String, Object>> thisTeaTeachTabList = relationDao.getRelaById(relDetail);
			if(thisTeaTeachTabList == null || thisTeaTeachTabList.size() == 0){
				Map<String, Object> courseDetail = relationDao.getInsertTeacherId(paramMap);
				courseDetail.put("inMark", 1);
				//如果录入人与这个课在班级里面有关联，不改inMark
				List<Map<String, Object>> attendList = relationDao.getClassRelationByCourseIdAndTeacherId(courseDetail);
				if(attendList == null || attendList.size() == 0){
					relationDao.saveInMarkReMark(courseDetail);
				}
			}
			//删除关联信息
			boolean relResult = this.deleteBeforeRel(paramMap);
//			//删除学生与班级的关联信息
//			if(relClassTeacherList != null && relClassTeacherList.size() != 0){
//				//如果课表长度为1，并且主键id是要删除的主键id，就剩一条了
//				if((relClassTeacherList.size() == 1) && ((Integer.parseInt(relClassTeacherList.get(0).get("relationId").toString())) == (Integer.parseInt(relDetail.get("relationId").toString())))){
//					//如果这条是选修的，将选修这个班级与学生之间的关联删除
//					if(Integer.parseInt(relDetail.get("selectMark").toString()) == 2){
//						Map<String, Object> detailMsg = new HashMap<String, Object>();
//						detailMsg.put("courseId", relDetail.get("courseId"));
//						detailMsg.put("myDelMark", 1);
//						relationDao.delSelectRel(detailMsg);
//					}
//				}else{
//					this.delStuReClass(paramMap, relDetail);
//				}
//			}
			if(relResult){
				//删除课程表
				relationDao.delClassTabById(paramMap);
				resultMap.put("result", true);
			}else{
				resultMap.put("result", false);
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询全部可选课课程
	 * @param Map<String,Object> paramMap  
	 * @return 
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSelectCourses(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = relationDao.getSelectCourses(paramMap);
		}catch(Exception e){
			throw e;
		}
		return list;
	}
	
	/**
	 * @description  查询全部已经排课了的教师的信息 
	 * @param Map<String,Object> paramMap  
	 * @return 
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSelectTeacherList(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = relationDao.getSelectTeacherList(paramMap);
		}catch(Exception e){
			throw e;
		}
		return list;
	}
	
	/**
	 * @description  选课课程的列表 
	 * @param Map<String,Object> paramMap  
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> getSelectList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> selectList = relationDao.getSelectList(paramMap);
			resultMap.put("selectList", selectList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = relationDao.getSelectList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description  保存学生选课信息
	 * @param Map<String,Object> paramMap  
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> saveStuSelectMsg(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//查询该学生之前的所有课表信息与当前选择的课程的这个类型的课表是否有时间相同地点不同的数据
			List<Map<String, Object>> repeatList = relationDao.getRepeatclassTab(paramMap);
			//如果没有继续执行
			if(repeatList == null || repeatList.size() == 0){
				//查询这个课程选了多少人，一共选择多少人
				String stuorteaId = (String)paramMap.get("stuorteaId");
				paramMap.remove("stuorteaId");
				List<Map<String, Object>> courseList = relationDao.getSelectList(paramMap);
				if(courseList != null && courseList.size() != 0){
					int maxStu = (Integer)courseList.get(0).get("maxStu");
					long selectNum = 0;
					selectNum = (courseList.get(0).get("stuNum") != null) ? ((Long)courseList.get(0).get("stuNum")) : 0;
					if(maxStu > selectNum){
						paramMap.put("inMark", 0);
						paramMap.put("stuorteaId", stuorteaId);
						paramMap.put("selectMark", 2);
						//判断之前有没有这个学生这个课的关联，如果没有，添加，如果有，修改标识为2
						List<Map<String, Object>> relStuCouList = relationDao.getRelCourseTea(paramMap);
						if(relStuCouList == null || relStuCouList.size() == 0){
							courseDao.addRelTeaCourse(paramMap);
						}else{
							//如果有学生课程的关联，需要将关系标识修改为2
							relationDao.saveSelectMark(paramMap);
							//将是否结课的标识改为0
							paramMap.put("endMark", 0);
							classDao.saveEndMark(paramMap);
						}
						//0510将学生与选课班级的关联加进去
						//查询这个学生与这个班级之前有没有过关联信息
						List<Map<String, Object>> selectClassMark = relationDao.getStuSelectClassList(paramMap);
						if(selectClassMark == null || selectClassMark.size() == 0){
							courseDao.addSelectRestuClass(paramMap);
						}
						resultMap.put("result", true);
					}else{
						resultMap.put("result", false);
						resultMap.put("failMsg", "课程人数已满");
					}
				}
			}else{
				resultMap.put("result", false);
				resultMap.put("failMsg", "当前选择课程与课表冲突，无法继续选择");
			}
			
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description  查询某条课程表的全部信息只要id就可以
	 * @param Map<String,Object> paramMap   relationId
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> getOneRelationById(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = relationDao.getOneRelationById(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description  修改一节课程表信息的
	 * @param Map<String,Object> paramMap   relationId 等全部课表信息
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> saveOneCourseTab(Map<String, Object> paramMap)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			//获取上课时间的参数
			Map<String, Object> paramTimeMap = paramActionImpl.matchClassNumAndTime();
			List<Map<String, Object>> paramList = new ArrayList<Map<String,Object>>();
			paramList.add(paramMap);
			//查询是否可以进行排课（地点是否冲突，教师是否有课）
			boolean saveMark = (Boolean)this.getSaveTabMark(paramList).get("result");
			//可以进行排课
			if(saveMark){
				paramMap.put("startTime", paramTimeMap.get((Integer)paramMap.get("classNum")+"start"));
				paramMap.put("endTime", paramTimeMap.get((Integer)paramMap.get("classNum")+"end"));
				//修改课表
				relationDao.saveClassTabOne(paramMap);
				map.put("result", true);
			}else{
				map.put("result", false);
				map.put("failMsg", "排课地点冲突或教师上课时间冲突");
			}
		}catch(Exception e){
			throw e;
		}
		return map;
	}
	
	/**
	 * @description  获取学生与课程的关联信息
	 * @param Map<String,Object> paramMap   relationId
	 * @return 
	 * @throws Exception
	 */
	public List<Map<String, Object>> getRelStuCourseListById(HttpServletRequest request) throws Exception{
		
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		try {
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request);
			resultList = relationDao.getRelStuCourseListById(sessionValues);
		} catch (Exception e) {
			throw e;
		}
		return resultList;
	}
	
	/**
	 * @description  给出该学生是否选择了课程标识
	 * @param Map<String,Object> paramMap   relationId
	 * @return 
	 * @throws Exception
	 */
	public List<Map<String, Object>> setSelectMark(List<Map<String, Object>> paramList,HttpServletRequest request) throws Exception{
		
		try {
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request);
			for(int i=0;i<paramList.size();i++){
				sessionValues.put("courseId", paramList.get(i).get("courseId"));
				List<Map<String, Object>> relationList = relationDao.getRelStuCourseListById(sessionValues);
				if(relationList != null && relationList.size() != 0){
					//0626查询这个血红色那个所在的全部班级与这个课的未结课的信息有没有，如果有就是选择过，不能选了，true
					List<Map<String, Object>> selectNotEndList = relationDao.getRelationStuSelectMark(sessionValues);
					if(selectNotEndList != null && selectNotEndList.size() != 0){
						paramList.get(i).put("selectMark", true);
						//0713查询这个学生与这个课的关系，selectMark
					    List<Map<String, Object>> stuSelectMarkList = relationDao.getRelCourseTea(sessionValues);
						if(stuSelectMarkList != null && stuSelectMarkList.size() != 0){
							if(Integer.parseInt(stuSelectMarkList.get(0).get("couSelectMark").toString()) == 1){
								paramList.get(i).put("selectMark", true);
								paramList.get(i).put("selectState", 1);
							}else{
								paramList.get(i).put("selectMark", true);
								paramList.get(i).put("selectState", 2);
							}
						}
						//0713end
					}else{
						paramList.get(i).put("selectMark", false);
					}
					//0626end
				}else{
					paramList.get(i).put("selectMark", false);
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return paramList;
	}

	/**
	 * @description  用于删除之前的关联信息的代码
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	*/
	public boolean deleteBeforeRel(Map<String, Object> paramMap) throws Exception{
		
		boolean result = false;
		try{
			//给是否删除学生与课程关联的标识
			boolean delStuReCourse = false;
			//查询这条课表全部信息
			Map<String, Object> relDetail = relationDao.getOneRelationById(paramMap);
			relDetail.put("type", 1);
			//如果是学生选课
			if(Integer.parseInt(relDetail.get("selectMark").toString()) != 1){
				//1、查询数据库课程表信息中全部这个课程的这个班级的课表信息
				List<Map<String, Object>> relCourseClassList = relationDao.getRelaById(relDetail);
				if(relCourseClassList != null && relCourseClassList.size() != 0){
					//如果课表长度为1，并且主键id是要删除的主键id，就删除中间表信息，这个课这个班级的
					if(relCourseClassList.size() == 1 && Integer.parseInt(relCourseClassList.get(0).get("relationId").toString()) == Integer.parseInt(relDetail.get("relationId").toString())){
						//删除课程与班级中间表信息  参数：relDetail
						relationDao.delCourseReClass(relDetail);
						delStuReCourse = true;
					}
				} 
				//2、查询数据库课程表信息中全部这个老师这个课程的课表信息
				relDetail.put("type", 2);
				List<Map<String, Object>> relCourseTeacherList = relationDao.getRelaById(relDetail);
				if(relCourseTeacherList != null && relCourseTeacherList.size() != 0){
					//如果课表长度为1，并且主键id是要删除的主键id，就删除中间表信息，这个课这个老师的
					if(relCourseTeacherList.size() == 1 && Integer.parseInt(relCourseTeacherList.get(0).get("relationId").toString()) == Integer.parseInt(relDetail.get("relationId").toString())){
						//删除课程与老师中间表信息  参数：relDetail
						relationDao.delCourseReTeacher(relDetail);
					}
				}
				//3、查询数据库课程表信息中全部这个老师这个班级的课表信息
				relDetail.put("type", 3);
				List<Map<String, Object>> relClassTeacherList = relationDao.getRelaById(relDetail);
				if(relClassTeacherList != null && relClassTeacherList.size() != 0){
					//如果课表长度为1，并且主键id是要删除的主键id，就删除中间表信息，这个班级这个老师的
					if(relClassTeacherList.size() == 1 && Integer.parseInt(relClassTeacherList.get(0).get("relationId").toString()) == Integer.parseInt(relDetail.get("relationId").toString())){
						//删除班级与老师中间表信息  参数：relDetail
						relationDao.delClassReTeacher(relDetail);
					}
				}
				//4、是否删除学生与课程的关联
				if(delStuReCourse){
					//删除学生与课程的关联信息
					relationDao.delStuReCourse(relDetail);
				}
			}
			result = true;
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description  修改课程的标识
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	*/
	public boolean saveCourseSelect(Map<String, Object> paramMap) throws Exception{
		
		boolean result = false;
		try{
			paramMap.put("selectType", 1);
			//查询该课程的正常课表
			List<Map<String, Object>> normalList = relationDao.selectTabList(paramMap);
			paramMap.put("selectType", 2);
			//查询该课程的选课课表    
			List<Map<String, Object>> selectList = relationDao.selectTabList(paramMap);
			//如果是选课和正常上课都有 ，就将标识存为3
			if(normalList != null && normalList.size() != 0 && selectList != null && selectList.size() != 0){
				paramMap.put("selectMark", 3);
				courseDao.saveSelectMark(paramMap);
			}
			result = true;
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description  删除剩余最后一种类型的这个课的排课信息的时候，删除关联信息
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	*/
	public boolean delByLastType(Map<String, Object> paramMap,Map<String, Object> detailMsg,List<Map<String, Object>> stuList) throws Exception{
		
		boolean result = false;
		try{
			//查询某个选课类型的课程的课程表条数
			detailMsg.put("myMark", 1);
			List<Map<String, Object>> oneMarkList = relationDao.getRelationBySetMark(detailMsg);
			detailMsg.put("myMark", 2);
			List<Map<String, Object>> twoMarkList = relationDao.getRelationBySetMark(detailMsg);
			//0619查询course的课程标识
			Map<String, Object> courseDetail = courseDao.getCourseDetail(detailMsg);
			//如果删除的是教师选课
			if(Integer.parseInt(detailMsg.get("selectMark").toString()) == 1){
				if(oneMarkList != null && oneMarkList.size() != 0){
					//如果只剩下一条，并且这条的主键与要删除的相同
					if(oneMarkList.size() == 1 && Integer.parseInt(paramMap.get("relationId").toString()) == Integer.parseInt(oneMarkList.get(0).get("relationId").toString())){
						//课程原来是3，修改为2
						if(Integer.parseInt(courseDetail.get("selectMark").toString()) == 3){
							Map<String, Object> saveUseParam = new HashMap<String, Object>();
							saveUseParam.put("selectMark", 2);
							saveUseParam.put("courseId", detailMsg.get("courseId"));
							courseDao.saveSelectMark(saveUseParam);
						}
						//0619 end
					}
				}
			//如果删除的是学生选课	
			}else if(Integer.parseInt(detailMsg.get("selectMark").toString()) == 2){
				if(twoMarkList != null && twoMarkList.size() != 0){
					//如果只剩下一条，并且这条的主键与要删除的相同
					if(twoMarkList.size() == 1 && Integer.parseInt(paramMap.get("relationId").toString()) == Integer.parseInt(twoMarkList.get(0).get("relationId").toString())){
						//删除学生与这个课的这个类型的关系
						relationDao.delStuReCourse(detailMsg);
						//将数据库中所有关于这个课程的选课的信息删除掉
						//1、删除学生与班级的关系  参数：detailMsg
						detailMsg.put("myDelMark", 1);
						relationDao.delSelectRel(detailMsg);
						//2、删除学生和老师与课程的关系  参数：detailMsg
						detailMsg.put("myDelMark", 2);
						relationDao.delSelectRel(detailMsg);
						//3、删除老师与教的班级之间的关联关系  参数：detailMsg
						detailMsg.put("myDelMark", 3);
						relationDao.delSelectRel(detailMsg);
						//4、删除这个选课的班级  参数：detailMsg
						detailMsg.put("myDelMark", 4);
						relationDao.delSelectRel(detailMsg);
						//5、删除课程与班级的关联关系  参数：detailMsg
						detailMsg.put("myDelMark", 5);
						relationDao.delSelectRel(detailMsg);
						//修改原来上课老师与课程的关联关系类型couSelectMark
						detailMsg.put("selectMark", 1);
						relationDao.saveSelectMarkAll(detailMsg);
						//原来是3，修改为1
						if(Integer.parseInt(courseDetail.get("selectMark").toString()) == 3){
							Map<String, Object> saveUseParam = new HashMap<String, Object>();
							saveUseParam.put("selectMark", 1);
							saveUseParam.put("courseId", detailMsg.get("courseId"));
							courseDao.saveSelectMark(saveUseParam);
						}
						//0619 end
					}
				}
			}
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description  查询某个课表某节里面对应的选课班级，十九查询出来不是就无结果
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	*/
	public Map<String, Object> getSelectClassOne(Map<String, Object> paramMap) throws Exception{
	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try{
			resultMap = relationDao.getSelectClassOne(paramMap);
		}catch(Exception e){
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description  生成选课班级名称
	 * 规则：用课程名称+当前2位日期+申请班  如果有这个名字的班级，就用课程名+当前时间戳
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	*/
	public String getRandomClassName(Map<String, Object> paramMap) throws Exception{
		String result = null;
		try{
			Date dt=new Date();
		    SimpleDateFormat matter1=new SimpleDateFormat("dd");
		    String day = matter1.format(dt);
		    String timestamp = String.valueOf(System.currentTimeMillis());
			//通过课程id获取课程名字信息
		    Map<String, Object> courseDetail = courseDao.getCourseDetail(paramMap);
			String courseName = (String)courseDetail.get("courseName");
			String oneSelect = courseName + day + "申请班";
			List<Map<String, Object>> classList = classDao.getClassByClassName(oneSelect);
			//如果存在这个名字的班级，就更换下一个
			if(classList != null && classList.size() != 0){
				result = courseName + timestamp;
			}else{
				result = oneSelect;
			}
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description  查询全部教师与课程之间的关系列表
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	*/
	public Map<String, Object> getTeaReCourseList(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取老师与课程的列表信息
			List<Map<String, Object>> reList = relationDao.getTeaReCourseList(paramMap);
			returnMap.put("reList", reList);
			returnMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allReList = relationDao.getTeaReCourseList(paramMap);
			int totalPage = pageUtil.getTotalPage(allReList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * @description  解绑方法 ，教师与课程之间的解绑方法
	 * @param Map<String,Object>   传课程id，教师id，类型
	 * @return 
	 * @throws Exception
	 */
	public boolean unbundlingRelation(Map<String, Object> paramMap) throws Exception {

		boolean result = false;
		try {
			result = this.unbindFunction(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 课程库
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> courseList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取老师与课程的列表信息
			List<Map<String, Object>> courseList = relationDao.courseList(paramMap);
			returnMap.put("courseList", courseList);
			returnMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allReList = relationDao.courseList(paramMap);
			int totalPage = pageUtil.getTotalPage(allReList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * @description 查询这个班级中的全部学生，并且查询出来每个学生拥有的虚拟机集合
	 * @param Map<String,Object> 
	 * @return 
	 * @throws Exception
	 */
	public Map<String, Object> getClassVirtualList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取这个班级下的学生信息
			List<Map<String, Object>> stuList = relationDao.getStuList(paramMap);
			if(stuList != null){
				List<Map<String, Object>> list;
				Map<String,Object> idMap ;
				for(int i = 0; i < stuList.size(); i++){
					list = new ArrayList<Map<String,Object>>();
					idMap = new HashMap<String, Object>();
					idMap.put("stuorteaId", stuList.get(i).get("stuorteaId"));
					//查询每个学生具有的虚拟机信息列表
					//获取我的虚拟机信息，需要用是不是主机的字段进行排序，主机在上从在下
					list = myCourseDao.getMyvirtualAddress(idMap);
					stuList.get(i).put("virtualList", list);
				}
			}
			returnMap.put("stuList", stuList);
			returnMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allReList = relationDao.getStuList(paramMap);
			int totalPage = pageUtil.getTotalPage(allReList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * @description 用于解绑教师与这个班级这个课程关联的方法
	 * @param Map<String,Object> 
	 * @return boolean
	 * @throws Exception
	 */
	public boolean unbindFunction(Map<String, Object> paramMap) throws Exception{
	
		boolean result = false;
		try{
			//如果类型是1
			if(Integer.parseInt(paramMap.get("couSelectMark").toString()) == 1){
				//删除classrelation表中的关联信息
				classDao.delClassRelation(paramMap);
				//删除这个班级下的学生，这个课，这个类型的关联信息relstucou
				classDao.delStuReCouByClassId(paramMap);
			}
			//如果是类型2，删除这个选课班级下的学生与这个课的关联
			if(Integer.parseInt(paramMap.get("couSelectMark").toString()) == 2){
				//删除这个班级下的学生，与这个课，这个类型的关联
				classDao.delStuReCouByClassId(paramMap);
				//删除选课班级
				paramMap.put("myDelMark", 4);
				relationDao.delSelectRel(paramMap);
			}
			//查询这个课的全部任课教师信息
			List<Map<String, Object>> teacherList = relationDao.getCourseTeacher(paramMap);
			//如果数据库只有一条任课信息，并且，老师是参数这个老师
			if(teacherList.size() == 1 
					&& ((String)teacherList.get(0).get("stuorteaId")).equals((String)paramMap.get("stuorteaId")))
			{
				//course标识修改为正常的课
				Map<String, Object> paramUseMap = new HashMap<String, Object>();
				paramUseMap.put("selectMark", 1);
				paramUseMap.put("courseId", paramMap.get("courseId"));
				courseDao.saveSelectMark(paramUseMap);
				
				//如果这个老师不教别的班级这个课，这个类型的了，就删除，
				List<Map<String, Object>> classList = relationDao.getTeacherClasses(paramMap);
				//如果不教别的班级了，删除这个老师的所有课表
				if(classList == null || classList.size() == 0){
					//删除这个课程的这个类型的课表，参数是课id、类型
					relationDao.delRelation(paramMap);
				}
			//还有别的任课教师	
			}else{
				//修改course课程的标识类型
				this.saveCourseMark(paramMap);
				//查询当前这个老师，这个类型的，除了这个班，还教不教别的班级
				List<Map<String, Object>> classList = relationDao.getTeacherClasses(paramMap);
				//如果不教别的班级了，删除这个老师的所有课表
				if(classList == null || classList.size() == 0){
					relationDao.delRelationTabByTeacherId(paramMap);
				}
				
			}
			//删除这个课这个班级的关联
			relationDao.delCourseReClass(paramMap);
			//查询这个课程的录入人
			Map<String, Object> courseDetail= courseDao.getCourseDetail(paramMap);
			//如果是录入人
			if(((String)courseDetail.get("stuorteaId")).equals((String)paramMap.get("stuorteaId"))){
				//查询这个老师与这门课的关系，
				List<Map<String, Object>> relationMap = relationDao.getRelCourseTea(paramMap);
				if(relationMap != null){
					//如果只教选修，或只教正常的课，将inMark改为1，将couselectMark改为1
					if(Integer.parseInt(relationMap.get(0).get("couSelectMark").toString()) == 2 || Integer.parseInt(relationMap.get(0).get("couSelectMark").toString()) == 1){
						//修改录入人标识为1
						Map<String, Object> saveMarkMap = new HashMap<String, Object>();
						saveMarkMap.put("inMark", 1);
						saveMarkMap.put("courseId", paramMap.get("courseId"));
						saveMarkMap.put("stuorteaId", paramMap.get("stuorteaId"));
						relationDao.saveInMarkReMark(saveMarkMap);
						//修改录入人与课程的标识，couSelectMark
						saveMarkMap.put("selectMark", 1);
						relationDao.saveSelectMark(saveMarkMap);
					//如果这门课这个录入人两种都教，	
					}else if(Integer.parseInt(relationMap.get(0).get("couSelectMark").toString()) == 3){
						//如果删除的是选修的，变成1，如果删除的是正常的，变成2
						if(Integer.parseInt(paramMap.get("couSelectMark").toString()) == 1){
							Map<String, Object> saveMarkMap = new HashMap<String, Object>();
							saveMarkMap.put("courseId", paramMap.get("courseId"));
							saveMarkMap.put("stuorteaId", paramMap.get("stuorteaId"));
							//修改录入人与课程的标识，couSelectMark
							saveMarkMap.put("selectMark", 2);
							relationDao.saveSelectMark(saveMarkMap);
						}else{
							Map<String, Object> saveMarkMap = new HashMap<String, Object>();
							saveMarkMap.put("courseId", paramMap.get("courseId"));
							saveMarkMap.put("stuorteaId", paramMap.get("stuorteaId"));
							//修改录入人与课程的标识，couSelectMark
							saveMarkMap.put("selectMark", 1);
							relationDao.saveSelectMark(saveMarkMap);
						}
					}
				}
			//不是录入人	
			}else{
				//如果不教别的班级了，就删除这个老师与课程的关联，如果还教别的班级不删除
				//查询当前这个老师，这个类型的，这个课，除了这个班，还教不教别的班级
				List<Map<String, Object>> classList = relationDao.getTeacherClasses(paramMap);
				//如果不教别的班级了，删除这个老师与课程的关联信息
				if(classList == null || classList.size() == 0){
					//删除relstucou的这个老师，这个课的关联
					relationDao.delCourseReTeacher(paramMap);
				}
			}
			//删除这个老师与这个班级的关联信息，这个课的关联信息
			relationDao.delClassReTeacher(paramMap);
			//删除这个班级下的所有学生的这个课的作业信息
			homeworkTestDao.delTaskByCourseIdAndClassId(paramMap);
			//删除这个班级下的学生的这个课的成绩
			homeworkTestDao.delExamByClassIdAndCourseId(paramMap);
			//删除这个班级下的学生的学习历史信息，
			homeworkTestDao.delClassStudyHistoryByCourseId(paramMap);
			//如果老师这个课还教别的班级的信息
			List<Map<String, Object>> classesList = relationDao.getTeachClassByCourseId(paramMap);
			//如果这个教师，这个课，不教其他班级了，就删除历史信息
			if(classesList == null || classesList.size() == 0){
				//删除这个老师这个课的历史信息
				relationDao.delstudyHistoryByCourseIdAndTeacherId(paramMap);
			}
			//删除这个班级与学生的关系relstuclass(条件是选修的班级,正常的班级的话不删除)
			classDao.delRelstuclassByClassId(paramMap);
			result = true;
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 修改course课程的标识类型
	 * @param Map<String,Object>  课id，类型,班级id，老师id
	 * @return boolean
	 * @throws Exception
	 */
	public boolean saveCourseMark(Map<String, Object> paramMap) throws Exception{
	
		boolean result = false;
		try{
			Map<String, Object> paramUseMap = new HashMap<String, Object>();
			paramUseMap.put("stuorteaId", paramMap.get("stuorteaId"));
			paramUseMap.put("courseId", paramMap.get("courseId"));
			paramUseMap.put("couSelectMark", 1);
			//查询除了这个老师，这个课的正常上课的教师
			List<Map<String, Object>> normalList = relationDao.getCourseTeacherByType(paramMap);
			//查询除了这个老师，这个课选课的教师
			paramUseMap.put("couSelectMark", 2);
			List<Map<String, Object>> selectList = relationDao.getCourseTeacherByType(paramMap);
			//如果除了这个老师，只有正常上课的教师，课程改为正常
			if(normalList != null && normalList.size() != 0 && (selectList == null || selectList.size() == 0)){
				Map<String, Object> paramUseMapCourse = new HashMap<String, Object>();
				paramUseMapCourse.put("selectMark", 1);
				paramUseMapCourse.put("courseId", paramMap.get("courseId"));
				courseDao.saveSelectMark(paramUseMapCourse);
			//如果除了这个老师，只有选课的教师上课，改为选课的	
			}else if(selectList != null && selectList.size() != 0 && (normalList == null || normalList.size() == 0)){
				Map<String, Object> paramUseMapCourse = new HashMap<String, Object>();
				paramUseMapCourse.put("selectMark", 2);
				paramUseMapCourse.put("courseId", paramMap.get("courseId"));
				courseDao.saveSelectMark(paramUseMapCourse);
			}
			result = true;
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 获取这个学生，这个班级，这个课，是哪个老师教，
	 * @param Map<String,Object>  课id，类型,班级id，老师id
	 * @return boolean
	 * @throws Exception
	 */
	public Map<String, Object> getMyTeacherByCourseId(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			//如果是学生
			if(Integer.parseInt(paramMap.get("roleId").toString()) == 1){
				//查询他所在班级的这个课程的教课老师id
				List<Map<String, Object>> list = relationDao.getMyTeacherByCourseId(paramMap);
				if(list != null && list.size() != 0){
					map.put("stuorteaId", list.get(0).get("stuorteaId"));
				}
			}
		}catch(Exception e){
			throw e;
		}
		return map;
	}
	
}